package com.study.demo.dao.impl;

import com.study.demo.dao.UserDao;
import com.study.demo.po.User;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicLong;
@Repository
public class UserDaoImpl implements UserDao {
    private AtomicLong atomicLong=new AtomicLong();
    private ConcurrentMap<Long,User> userMap=new ConcurrentHashMap<>();
    @Override
    public List<User> selectList() {
        List<User> userList=new ArrayList<>();
        for(User user:userMap.values()){
            userList.add(user);
        }
        return userList;
    }

    @Override
    public User selectOne(Long id) {
        return userMap.get(id);
    }

    @Override
    public int saveOrUpdateUser(User user) {
        Long id=user.getId();
        if (user.getId()==null){
            id=atomicLong.incrementAndGet();
            user.setId(id);
        }
        userMap.put(id,user);
        return 1;
    }

    @Override
    public int deleteUser(Long id) {
        userMap.remove(id);
        return 1;
    }
}
